package edu.xpu.compute.recommend.restful;

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import edu.xpu.compute.recommend.common.model.PageModel;
import edu.xpu.compute.recommend.common.model.RequestListParam;
import edu.xpu.compute.recommend.common.model.ResponseParam;
import edu.xpu.compute.recommend.entity.SysUser;
import edu.xpu.compute.recommend.service.SysUserService;
import edu.xpu.compute.recommend.vo.SysUserExcelVo;

@RequestMapping(value="restful/user")
@RestController
public class SysUserRestful {
	@Autowired
	private SysUserService sysUserService;
	
	/**
	 * 导入学生信息
	 * @param file
	 * @return
	 * @throws IOException
	 */
	@PostMapping(value="importExcel")
	public ResponseParam<?> importExcel(@RequestParam("file") MultipartFile file) throws IOException {
		InputStream inputStream = file.getInputStream();
		ImportParams importParams = new ImportParams();
		try {
			List<SysUserExcelVo> importExcel = ExcelImportUtil.importExcel(inputStream, SysUserExcelVo.class, importParams);
			if (importExcel != null && importExcel.size() != 0) {
				Date currentDate = new Date();
				Integer sex = 0;
				for (SysUserExcelVo sysUserExcelVo : importExcel) {
					String sexStr = sysUserExcelVo.getSex();
					if ("M".equals(sexStr)) {
						sex = 1;
					} else {
						sex = 0;
					}
					SysUser sysUser = new SysUser(sysUserExcelVo.getNo(), "111111", sex, sysUserExcelVo.getNo(),
							sysUserExcelVo.getFacultyName(), sysUserExcelVo.getMajorName());
					sysUser.setCreateDate(currentDate);
					sysUser.setId(Long.parseLong(sysUserExcelVo.getNo()));
					sysUserService.save(sysUser);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return new ResponseParam<>(e.getMessage(), 500);
		}
		return new ResponseParam<>("上传成功", 200);
	}
	
	@GetMapping(value="findAll")
	public ResponseParam<List<SysUser>> findAll() {
		List<SysUser> findAll = sysUserService.findAll();
		return new ResponseParam<List<SysUser>>("数据获取成功", 200, findAll);
	}
	
	/**
	 * 获取分页列表信息
	 * @param requestListParam
	 * @return
	 * @throws UnsupportedEncodingException
	 */
	@PostMapping(value="list")
	public ResponseParam<PageModel<SysUser>> list(@RequestBody(required=false) RequestListParam<SysUser> requestListParam) {
		if (requestListParam == null) {
			requestListParam = new RequestListParam<>();
		}
		PageModel<SysUser> pageByConditions = sysUserService.getPageByConditions(requestListParam.getNumber()
													,requestListParam.getSize()
													,requestListParam.getEntity()
													,requestListParam.getSortField()
													,requestListParam.getSortType());
		return new ResponseParam<PageModel<SysUser>>("数据获取成功", 200, pageByConditions);
	}
	/**
	 * 根据id获取数据
	 * @param id
	 * @return
	 */
	@GetMapping(value="get/{id}")
	public ResponseParam<SysUser> get(@PathVariable(value="id") Long id) {
		SysUser sysUser = sysUserService.get(id);
		return new ResponseParam<SysUser>("数据获取成功", 200, sysUser);
	}
	/**
	 * 保存数据
	 * @param SysUser
	 * @return
	 */
	@PostMapping(value="save")
	public ResponseParam<SysUser> save(@RequestBody SysUser sysUser) {
		sysUser.setCreateDate(new Date());
		SysUser user = sysUserService.save(sysUser);
		return new ResponseParam<SysUser>("数据保存成功", 200, user);
	}
	/**
	 * 更新数据
	 * @param SysUser
	 * @return
	 */
	@PostMapping(value="update")
	public ResponseParam<SysUser> update(@RequestBody SysUser sysUser) {
		if (sysUser == null || sysUser.getId() != 0L) {
			return new ResponseParam<>("待更新数据没有主键", 500);
		}
		SysUser user = sysUserService.save(sysUser);
		return new ResponseParam<SysUser>("数据更新成功", 200, user);
	}
	/**
	 * 删除数据
	 * @param id
	 * @return
	 */
	@GetMapping(value="delete")
	public ResponseParam<String> delete(@RequestParam("ids") String ids) {
		try {
			String[] split = ids.split(",");
			for (String idStr : split) {
				sysUserService.deleteById(Long.parseLong(idStr));
			}
		} catch (Exception e) {
			return new ResponseParam<String>(e.getMessage(), 500);
		}
		return new ResponseParam<String>("数据删除成功", 200);
	}
}
